<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 29/06/11
 * Time: 22:29
 * To change this template use File | Settings | File Templates.
 */
require_once(Config::acquire('Controller', null, 'controller'));
require_once(Config::acquire('QuestionQueue', MODEL));
require_once(Config::acquire('Oplossing', MODEL));
class ScriptController extends Controller
{


    public function question()
    {
        $params = UrlDecoder::get_Params();

        if (isset($params[0])) {

            $queue = QuestionQueue::read($params[0]);
            if (is_null($queue->getSolution())) {
                $this->getView()->assign('form_URL', sds::getURL('Script', 'parseQuestion'));
                $this->getView()->assign('question', $queue->getParentQuestion()->getOmschrijving());
                $this->getView()->assign('questionid', $queue->getParentQuestion()->getId());
                $this->getView()->assign('true_id', $queue->getChildQuestionTrue());
                $this->getView()->assign('false_id', $queue->getChildQuestionFalse());
            } else {
                $this->getView()->assign('afhandel_link', sds::getURL('Script', 'parseAfhandeling'));
                $this->getView()->assign('overzicht_link', SDS::getURL('Incident', 'overzicht'));
                $this->getView()->assign('oplossing', Oplossing::read($queue->getSolution())->getOmschrijving());
                $this->getView()->assign('questionid', $queue->getParentQuestion()->getId());
            }
        }
        $this->getView()->render('Question_Queue.tpl');
    }

    public function parseQuestion()
    {
        $answer = QuestionQueue::read($_POST['questionid'])->getChildQuestionTrue() == $_POST['child'] ? 'ja' : 'nee';
        $incident_id = Register::get('incident', true);
        SQL::execute("INSERT INTO `incident_history` (
            `History_ID` ,
            `Incident_ID` ,
            `Question_ID` ,
            `Question_Answer`)
        VALUES (    NULL , '$incident_id', '" . $_POST['questionid'] . "', '$answer');");
        SDS::redirect('Script', 'question', array($_POST['child']));


    }

    public function parseAfhandeling()
    {
        require_once(Config::acquire('Incident', MODEL));
        $id = Register::get('incident', true);
        Register::unset_regiseter('incident');
        Incident::read($id)->Afhandelen();
        SDS::redirect('Incident', 'detail',array($id));
    }

    /*
    public function question(){
        $params =UrlDecoder::get_Params();

        if(isset($params[0])){
           $queue =   QuestionQueue::read($params[0]);

            $this->getView()->assign('question',$queue->getParentQuestion()->getOmschrijving());
             $this->getView()->assign('true_id',$queue->getChildQuestionTrue());
             $this->getView()->assign('false_id',$queue->getChildQuestionFalse());

        }
        SDS::redirect('Script',null);
    }
    */
    public function currentQuestion()
    {
        $params = UrlDecoder::get_Params();

        if (isset($params[0])) {
            $this->view->assign('question', QuestionQueue::read($params[0])->toArray());
        }
    }

    public function saveQuestion()
    {
        require_once(Config::acquire('FormValidation', APPLICATION));
        FormValidation::stripInput();
        Incident_history::add(null,
                              $_POST['Incident_ID'],
                              $_POST['Question_ID'],
                              $_POST['Question_Answer'])->update();
        SDS::redirect("Question", "overzicht");
    }

    public function add()
    {
        // TODO: Implement add() method.
    }

    public function bewerken()
    {
        // TODO: Implement bewerken() method.
    }

    public function delete()
    {
        // TODO: Implement delete() method.
    }

    public function index()
    {
        $this->view->render("QuestionQueue.tpl");
    }

    public function overzicht()
    {
        // TODO: Implement overzicht() method.
    }

    public function toevoegen()
    {
        // TODO: Implement toevoegen() method.
    }

    public function update()
    {
        // TODO: Implement update() method.
    }
}
